﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Web.UI.HtmlControls;
using QzEbook.AccessObject;
using QzEbook.Common;
using QzEbook.Admin;

public partial class module_postMessage : System.Web.UI.UserControl
{
    private int bookId;
    private StringBuilder sb;
    private SessionUser userSession;
    protected void Page_Load(object sender, EventArgs e)
    {
        bookId=int.Parse(Request["Bid"]);
        bindData();
    }

    private void bindData()
    {
        userSession = Session[SessionManager.LOGINUSER] as SessionUser;
        if (userSession != null)
        {
            userNameTb.Text = userSession.Name;
        }

        Comment comment = new Comment();
        comment.BookId = bookId;
        List<Comment> list=comment.selectList();

        sb = new StringBuilder();
        int i = 0;
        foreach (Comment item in list)
        {
            if ((i++ % 2)==0)
            {
                sb.AppendLine("<dl>");
            }
            else
            {
                sb.AppendLine("<dl class='spacing'>"); 
            }
            sb.AppendFormat("<dt id=\"p{2}\">{0} <span> 发表于: {1}</span></dt>", item.UserName,item.Date,item.Id);
            sb.AppendFormat("<dd class=\"textinner\"><a href=\"user.xhtml?Uid={0}\" title=\"查看用户\"><img src=\"images/usersUpload/noset.gif\" alt=\"个性头像\"></a>{1}<dd>",item.UserId, item.Content);
            sb.AppendFormat("<dd class=\"textfooter\">{0}</dd>", item.Ip);
            sb.AppendLine("</dl>");
        }
    }


    private bool check(string userEnterString)
    {
        bool back = true;
        if (Request.Cookies["CheckCode"] == null)
        {
            // string error= "您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
            back = false;
        }
        if (String.Compare(Request.Cookies["CheckCode"].Value, userEnterString, true) != 0)
        {
            back = false;
        }
        return back;
    }
    protected void sendBtn_Click(object sender, EventArgs e)
    {
        if (!check(vCodeTb.Text.Trim()))
        {
            return;
        }
        string name=userNameTb.Text.Trim();
        string commentMsg=msgTb.Text.Trim();
        if (name.Length < 3||commentMsg.Length<3)
        {
            return;
        }

        Comment comment = new Comment();
        comment.BookId = bookId;
        comment.UserId = userSession.Uid;
        comment.Ip=Request.UserHostAddress;
        comment.Content = commentMsg;

        if (comment.insert())
        {
           Response.Redirect(Request.Url.AbsoluteUri);
        }

    }

    protected override void Render(HtmlTextWriter writer)
    {
        
        writer.Write("<div id=\"messageList\">");
        writer.Write(sb.ToString());
        
        if ( userSession== null)
        {
            sb = new StringBuilder();
            
            sb.Append("<div id=\"errorInfo\">会员可留言,请<a href=\"admin/Register.xhtml\" title=\"注册\">注册</a>,");
            string path = string.Format("{0}#ToMessage", Request.Url.PathAndQuery);

            sb.AppendFormat("如果你已有账户,请<a href=\"admin/Login.xhtml?raw={0}\" title=\"登陆\">登陆</a></div>", Server.UrlEncode(path));
            sb.Append("</div>");

            writer.Write(sb.ToString());
            return;
        }
        base.Render(writer);
        writer.Write("</div>");
    }
}
